package com.extra;

public class Solution922 {
    public int[] sortArrayByParityII(int[] nums) {
        int i0 = 0;
        int i1 = 1;
        while (i0 < nums.length && i1 < nums.length) {
            if (nums[i0] % 2 == 0) {
                i0 += 2;
            } else {
                while (i1 < nums.length && nums[i1] % 2 == 1) i1 += 2;
                swap(nums, i0, i1);
            }
        }
        return nums;
    }

    public void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
